package com.dkm.ec.product.domain;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import net.sf.json.JSONArray;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.dkm.base.core.context.OnlineUserIdHolder;
import com.dkm.base.core.helper.SpringHelper;
import com.dkm.base.core.util.string.StringUtils;
import com.dkm.biz.domain.AbstractDomain;
import com.dkm.biz.gl.persistence.entity.GlConfigPo;
import com.dkm.biz.gl.repository.GlConfigRepository;
import com.dkm.ec.product.persistence.dao.ProductMediaDao;
import com.dkm.ec.product.persistence.entity.ProductMediaPo;
import com.dkm.ec.product.repository.ProductMediaRepository;

/**
 * 对象功能:产品媒体 领域对象实体
 * 开发公司:SKG.COM
 * 开发人员:颜超敏
 * 创建时间:2014-12-17 17:21:14
 */
@Service
@Scope("prototype")
public class ProductMedia extends AbstractDomain<String, ProductMediaPo>{
	@Resource
	 private ProductMediaDao productMediaDao ;
	@Resource
	private ProductMediaRepository productMediaRepository;
	@Resource
	private GlConfigRepository glConfigRepository;

	protected Logger logger = LoggerFactory.getLogger(this.getClass());		
	protected void init(){
		productMediaDao = SpringHelper.getBean(ProductMediaDao.class);
		setDao(productMediaDao);
	}	 
	public String saveMedias(String productMediaJson,String productId) {
		//先删除旧的媒体
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("prodId", productId);
		logger.info(OnlineUserIdHolder.getUserId()+": ProductMedia saveMedias（String，String） 删除媒体:prodId="+productId);
		productMediaDao.deleteByKey("deleteByProdId", params);
		
		String homeMediaId = "";
		
		//再保存一次
		if(StringUtils.isNotEmpty(productMediaJson)){
			JSONArray jarrry = JSONArray.fromObject(productMediaJson);
			List<ProductMediaPo> productMediaPos = JSONArray.toList(jarrry, ProductMediaPo.class);
			for(ProductMediaPo productMediaPo:productMediaPos){
				productMediaPo.setProdId(productId);
				productMediaPo.setId(getIdGenerator().genSid());
				productMediaDao.create(productMediaPo);
				
				if(productMediaPo.getIsHomeType().equals("Y")){
					homeMediaId = productMediaPo.getId();
				}
			}
		}
		
		return homeMediaId;
	}
	
	public String saveMedias(List<ProductMediaPo> productMediaPos,String productId,String type) {
		//先删除旧的媒体
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("prodId", productId);
		params.put("cloudKey", type);
		logger.info(OnlineUserIdHolder.getUserId()+":ProductMedia saveMedias(List<ProductMediaPo>,String ) 删除媒体:prodId="+productId);
		productMediaDao.deleteByKey("deleteByProdId", params);
		
		String homeMediaId = "";
		
		//再保存一次
		if(productMediaPos!=null){
			for(ProductMediaPo productMediaPo:productMediaPos){
				
				productMediaPo.setCloudType(type);
				productMediaPo.setProdId(productId);
				productMediaPo.setId(getIdGenerator().genSid());
				productMediaDao.create(productMediaPo);
				
				if("Y".equals(productMediaPo.getIsHomeType())){
					homeMediaId = productMediaPo.getId();
				}
			}
		}
		
		return homeMediaId;
	}
	
	public List<ProductMediaPo> findAllByProdIdForSale(String prodId,String isGallery,String callType,String picType,String mediaType,String attrOpId){
		List<ProductMediaPo> productMediaPos = productMediaRepository.findByProdIdForSale(prodId,isGallery,mediaType,attrOpId);
		GlConfigPo glConfigPo = glConfigRepository.getByConfigKey("DetailedProductMediaSize");
		if(StringUtils.isEmpty(picType))picType="medium";
		String imgSize = glConfigPo.getImgSize(callType, picType);
		for(ProductMediaPo productMediaPo:productMediaPos){
			productMediaPo.setCloudPath(productMediaPo.getCloudPath()+imgSize);
		}
		return productMediaPos;
	}
	
	public List<ProductMediaPo> findAllByProdIdForSale(String prodId,String isGallery,String mediaType,String attrOpId){
		return  productMediaRepository.findByProdIdForSale(prodId,isGallery,mediaType,attrOpId);
	}
	
}
